package com.hxd.charitysystem.ui.fragment

import com.hxd.charitysystem.adapter.CardItemAdapter.CardPagerAdapter
import com.hxd.charitysystem.adapter.CardItemAdapter.ShadowTransformer
import com.hxd.charitysystem.base.common.MyLazyFragment
import com.hxd.charitysystem.bean.HomeCardItemListBean
import com.hxd.charitysystem.ui.activity.DetailsActivity
import com.hxd.charitysystem.ui.activity.MainActivity
import com.hxd.charitysystem.ui.activity.StartFundraisingActivity
import com.hxd.charitysystem.utils.DemoDataProvider
import com.hxd.charitysystem.utils.L
import com.hxd.charitysystem.utils.ProjectUrl
import com.hxd.tabactivityfragment.util.GsonToBean
import com.hxd.tabactivityfragment.util.ThreadUtil
import com.kongzue.dialog.v3.MessageDialog
import com.kymjs.rxvolley.client.HttpCallback
import com.vondear.rxtool.RxSPTool
import com.xuexiang.xui.widget.banner.widget.banner.base.BaseBanner
import com.xuexiang.xutil.tip.ToastUtils
import kotlinx.android.synthetic.main.fragment_home.*
import org.jetbrains.anko.support.v4.startActivity


/**
 *  项目名：    CharitySystem
 *  包名：      com.hxd.charitysystem.ui.fragment
 *  文件名:     HomeFragment
 *  创建者:     HXD
 *  创建时间:   2019/4/28 13:41
 *  描述:       主页模块
 */

open class HomeFragment : MyLazyFragment<MainActivity>() {

    private val mData by lazy { DemoDataProvider.getDatas() }
    private var mCardShadowTransformer: ShadowTransformer? = null
    private var mCardAdapter:CardPagerAdapter? = null

    //设置单例静态方法
    companion object {
        fun newInstance(): HomeFragment {
            return HomeFragment()
        }
    }

    override fun getLayoutId(): Int {
        return com.hxd.charitysystem.R.layout.fragment_home
    }

    override fun getTitleBarId(): Int {
        return 0
    }

    override fun initView() {
        initBanner()
        /**
         * 立即筹款按钮监听
         */
        btn_Fundraising_home.setOnClickListener {
            //判断是否登录了
            if (RxSPTool.getString(context, "token") != "") {
                startActivity<StartFundraisingActivity>()
            } else {
                MessageDialog.show(mActivity, "提示", "请先登陆后再操作", "确定")
            }

        }


    }

    /**
     * 初始化card数据
     */
    private fun initCardPagerItem() {
        mCardAdapter = CardPagerAdapter()
        ProjectUrl.getHomeCardItem(object : HttpCallback() {
            override fun onSuccess(result: String?) {
                val data = GsonToBean.JsonToBean<HomeCardItemListBean>(result)
                if (data.status == 200) {
//                    mViewPager.adapter = data.msg
                    ThreadUtil.runOnMainThread(Runnable {

                        data.msg.forEach { cardItem ->
                            mCardAdapter!!.addCardItem(cardItem)
                        }
                        mCardShadowTransformer = ShadowTransformer(mViewPager, mCardAdapter)
                        mCardShadowTransformer!!.enableScaling(true)
                        mViewPager.adapter = mCardAdapter
                        mViewPager.setPageTransformer(false, mCardShadowTransformer)
                        mViewPager.offscreenPageLimit = 3
                        mViewPager.currentItem = (mCardAdapter!!.count) / 2


                    })

                    L.i("首页获取数据成功 ${data.msg}")
                } else {
                    L.i("首页获取数据失败 ${data.msg}")
                }
            }

            override fun onFailure(errorNo: Int, strMsg: String?) {
                L.i("首页获取数据失败 errorNo:$errorNo, strMsg:$strMsg")
            }
        })

        mCardAdapter!!.setOnCardItemClickListener {
            startActivity<DetailsActivity>("singleCard" to it)
        }
    }

    /**
     * 初始化轮播图
     */
    private fun initBanner() {
        sib_corner_rectangle
            .setSource(mData)
            .setOnItemClickL(BaseBanner.OnItemClickL { position -> ToastUtils.toast("position--->$position") })
            .startScroll()
    }

    override fun initData() {

    }

    override fun onResume() {
        super.onResume()
        initCardPagerItem()
    }

}